#include "antlr_wrapper.hpp"

antlr_wrapper::antlr_wrapper(string fileName) {
	
	input = antlr3FileStreamNew((pANTLR3_UINT8) fileName.c_str(), ANTLR3_ENC_8BIT);
	lex = WhileLangLexerNew(input);
	tokens = antlr3CommonTokenStreamSourceNew(ANTLR3_SIZE_HINT, TOKENSOURCE(lex));
	parser = WhileLangParserNew(tokens);
}

program* antlr_wrapper::run() {
	program *p = parser->prog(parser);	// Match the prog-rule
	return p;
}

antlr_wrapper::~antlr_wrapper() {
	parser->free(parser);
	tokens->free(tokens);
	lex->free(lex);
	input->close(input);
}

arith* parse_as_arith(string code) 
{
	pANTLR3_INPUT_STREAM input = antlr3StringStreamNew((pANTLR3_UINT8)code.c_str(), ANTLR3_ENC_8BIT, code.size(), (pANTLR3_UINT8)"");
	pWhileLangLexer lex = WhileLangLexerNew(input);
	pANTLR3_COMMON_TOKEN_STREAM tokens = antlr3CommonTokenStreamSourceNew(ANTLR3_SIZE_HINT, TOKENSOURCE(lex));
	pWhileLangParser parser = WhileLangParserNew(tokens);
	
	arith *a = parser->aexpr(parser);	// Match the aexpr-rule
	
	parser->free(parser);
	tokens->free(tokens);
	lex->free(lex);
	input->close(input);

	return a;
}
